<?php
/**
 * 后台公共操作
 * @author zzdboy
 *
 */
class PublicAction extends Action {
	
	public function _initialize() {
	
	}
	
	/**
	 * 登陆
	 */
	public function login() {
		if ($this->isPost ()) {
			$username = $this->_post ( "username" );
			$password = $this->_post ( "password" );
			$verify = $this->_post ( "verify" );
			
			if ($_SESSION ['verify'] != md5 ( $verify )) {
				$this->error ( '验证码错误！', '/Public/login/' );
			} else {
				$Admin = D ( "Admin" );
				$where ['username'] = $_POST ['username'];
				$where ['password'] = md5 ( $_POST ['password'] );
				$AdminInfo = $Admin->where ( $where )->find ();
				if (! $AdminInfo) {
					$this->error ( '用户名或密码不正确！', '/Public/login/' );
				} else {
					import ( "ORG.Util.Session" );
					Session::set ( "admin_uid", $AdminInfo ['uid'] );
					
					// 更新登陆记录
					$data = array (
							'lastlogintime' => time (),
							'lastloginip' => get_client_ip () 
					);
					$Admin->where ( "uid = " . $AdminInfo ['uid'] )->save ( $data );
					$this->success ( "登陆成功！", "/" );
				}
			}
		} else {
			import ( "ORG.Util.Session" );
			$this->display ( "Public:login" );
		}
	}
	
	/**
	 * 修改密码
	 */
	public function changePwd() {
		if ($this->isPost ()) {
		
		} else {
		
		}
	}
	
	/**
	 * 退出
	 */
	public function logout() {
		import ( "ORG.Util.Session" );
		if (Session::is_set ( 'admin_uid' )) {
			Session::clear ();
			$this->success ( "注销成功！", '/Public/login/' );
		} else {
			$this->error ( '已经注销！', '/Public/login/' );
		}
	}
	
	/**
	 * 验证码
	 */
	public function verify() {
		import ( "ORG.Util.Image" );
		Image::buildImageVerify ( 4, 1, 'gif', '60px', '20px' );
	}
	
	/**
	 * 后台地图
	 */
	public function map() {
		
		$Admin = D ( "Admin" );
		$array = $Admin->admin_menu ( 0 );
		$map = "";
		$n = 1;
		foreach ( $array as $k => $v ) {
			if ($n == 1) {
				$map .= "<div class=\"map-menu lf\">";
			}
			$map .= "<ul>";
			$map .= "<li class=\"title\">{$v['name']}</li>";
			$childmenus = $Admin->admin_menu ( $v ['id'] );
			foreach ( $childmenus as $key => $r ) {
				$map .= "<li class=\"title2\">{$r['name']}</li>";
				$menus = $Admin->admin_menu ( $r ['id'] );
				foreach ( $menus as $s => $r ) {
					$map .= "<li><a href=\"javascript:Go({$r['id']},'{$r['controller']}')\">{$r['name']}</a></li>";
				}
			}
			$map .= "</ul>";
			if ($n % 2 == 0) {
				$map .= "</div><div class=\"map-menu lf\">";
			}
			$n ++;
		}
		
		$this->assign ( 'map', $map );
		
		$this->display ( "Public:map" );
	}
	
	/**
	 * 后台消息提示
	 */
	public function message() {
	
	}
	
	/**
	 * 锁屏
	 */
	public function screenlock() {
		import ( "ORG.Util.Session" );
		Session::set ( "lock_screen", 1 );
	}
	
	/**
	 * 登陆锁定
	 */
	public function loginscreenlock() {
		$lock_password = $this->_post ( "lock_password" );
		
		if (empty ( $lock_password )) {
			echo '2';
			exit ();
		}
		
		import ( "ORG.Util.Session" );
		
		$Admin = D ( "Admin" );
		$where ['uid'] = Session::get ( "admin_uid" );
		$AdminInfo = $Admin->where ( $where )->find ();
		
		if (md5 ( $lock_password ) != $AdminInfo ['password']) {
			echo '3';
			exit ();
		} else {
			Session::set ( "lock_screen", 0 );
			echo "1";
			exit ();
		}
	}
}
?>